<?php
/* $Id$ */
use Buan\Config;
use Buan\UrlCommand;

$urlRoot = Config::get('ext.Scribble.urlRoot');
ScribbleAdminView::getGlobalView()->addStylesheets("{$urlRoot}/css/scribble/page/index.css");
ScribbleAdminView::getGlobalView()->addJavascripts("{$urlRoot}/js/scribble/page/index.js");

/**
* Function for recursively rendering nodes in the Page tree.
*
* @param PageModel Page to render
* @return string
*/
function renderPageNode($page) {
	$v = new ScribbleAdminView();
	$v->loadHelper('html');
	$v->helpers->html->setUrlSuffix(Config::get('ext.Scribble.urlSuffix'));

	$pData = array(
		'id'=>$page->id,
		'name'=>$page->name,
		'isindex'=>$page->isindex,
		'datepublish'=>$page->datepublish,
		'dateexpire'=>$page->dateexpire,
		'_childcount'=>$page->_childcount,
		'_ispublished'=>$page->isPublished(),
		'_hasexpired'=>$page->hasExpired(),
	);
	$html  = '<li>';
	$html .= '<input rel="data" type="hidden" value="'.$v->helpers->html->e(json_encode($pData), FALSE).'" />';
	$html .= '<a href="'.$v->helpers->html->eUrl(UrlCommand::createUrl('scribble', 'page', $page->id), FALSE).'" class="edit" title="Edit page, &#34;'.$v->helpers->html->e($page->name, FALSE).'&#34;">'.$v->helpers->html->e($page->name, FALSE).'</a>';
	if($page->_childcount>0) {
		$html .= ' <a href="'.$v->helpers->html->eUrl(UrlCommand::createUrl('scribble', 'page', array('p'=>$page->id)), FALSE).'" title="Load pages within &#34;'.$v->helpers->html->e($page->name, FALSE).'&#34;" class="toggle"><span>(subpages)</span></a>';
	}
	if(!$page->_children->isEmpty()) {
		$html .= '<ul>';
		foreach($page->_children as $c) {
			$html .= renderPageNode($c);
		}
		$html .= '</ul>';
	}
	$html .= '</li>';
	return $html;
}
?>

<div class="head">
	<h1>Pages</h1>
</div>

<?php if($pages->isEmpty()): ?>
	<p class="information">
		You have no pages! <a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'page', 'create')); ?>">Create a page now</a> to start populating your website.
	</p>
<?php return 1; endif; ?>

<ul id="pages">
<?php foreach($pages as $p): ?>
	<?php echo renderPageNode($p); ?>
<?php endforeach; ?>
</ul>